#include <bits/stdc++.h>

using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution
{
public:
    vector<int> travesal(TreeNode *root)
    {
        vector<int> res;
        if (root == nullptr)
        {
            return res;
        }
        queue<TreeNode *> que;
        que.push(root);
        while (!que.empty())
        {
            int size = que.size();
            for (int i = 0; i < size; i++)
            {
                TreeNode *cur = que.front();
                que.pop();
                res.push_back(cur->val);
                if (cur->left)
                {
                    que.push(cur->left);
                }
                if (cur->right)
                {
                    que.push(cur->right);
                }
            }
        }
        return res;
    }

    // TreeNode *trimBST(TreeNode *root, int low, int high)
    // {
    //     if (root == nullptr)
    //     {
    //         return root;
    //     }
    //     if (root->val >= high)
    //     {
    //         if (root->val == high)
    //         {
    //             root->left = trimBST(root->left, low, high);
    //             root->right = nullptr;
    //             return root;
    //         }
    //         else
    //         {
    //             root->left = trimBST(root->left, low, high);
    //             return root->left;
    //         }
    //     }
    //     if (root->val <= low)
    //     {
    //         if (root->val == low)
    //         {
    //             root->right = trimBST(root->right, low, high);
    //             root->left = nullptr;
    //             return root;
    //         }
    //         else
    //         {
    //             root->right = trimBST(root->right, low, high);
    //             return root->right;
    //         }
    //     }
    //     else
    //     {
    //         root->left = trimBST(root->left, low, high);
    //         root->right = trimBST(root->right, low, high);
    //     }
    //     return root;
    // }

    // TreeNode *trimBST(TreeNode *root, int low, int high)
    // {
    //     if (root == nullptr)
    //     {
    //         return nullptr;
    //     }
    //     if (root->val < low)
    //     {
    //         TreeNode *right = trimBST(root->right, low, high);
    //         return right;
    //     }
    //     if (root->val > high)
    //     {
    //         TreeNode *left = trimBST(root->left, low, high);
    //         return left;
    //     }
    //     root->left = trimBST(root->left, low, high);
    //     root->right = trimBST(root->right, low, high);
    //     return root;
    // }

    TreeNode *trimBST(TreeNode *root, int low, int high)
    {
        if (root == nullptr)
        {
            return nullptr;
        }
        while (root != nullptr && (root->val < low || root->val > high))
        {
            if (root->val < low)
            {
                root = root->right;
            }
            else
            {
                root = root->left;
            }
        }
        TreeNode *cur = root;
        while (cur)
        {
            while (cur->left && cur->left->val < low)
            {
                cur->left = cur->left->right;
            }
            cur = cur->left;
        }
        cur = root;
        while (cur)
        {
            while (cur->right && cur->right->val > high)
            {
                cur->right = cur->right->left;
            }
            cur = cur->right;
        }
        return root;
    }
};

int main()
{
    TreeNode *root = new TreeNode(1);
    root->left = new TreeNode(0);
    root->right = new TreeNode(2);
    Solution sol;
    TreeNode *res = sol.trimBST(root, 1, 2);
    vector<int> vec = sol.travesal(res);
    for (auto num : vec)
    {
        cout << num << " ";
    }
    cout << endl;
    return 0;
}